package cn.devices.controller;


import cn.devices.entity.TbDevice;
import cn.devices.model.JsonMessage;
import cn.devices.service.TbDeviceService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 设备表 前端控制器
 * </p>
 * 添加设备信息: tbDevicesAdd  设备编号手动输入 ,type_id 设别类别必+
 * 设备更新该嗯新：tbDeviceUpdate
 * 编号修改：tbDeviceCodeUpdate
 * 设备列表： tbDeviceList
 * 单个设备信息：tbDevice
 * 删除设备信息：tbDeviceDelete
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */

@RestController
@RequestMapping("/tb-device")
public class TbDeviceController {
    @Value("${load.upload}")
    private String uploadPath;
    @Autowired
    private TbDeviceService tbDeviceService;

    @GetMapping("getByCode")
    public JsonMessage getByCode(String deviceCode) {
        JsonMessage message = new JsonMessage();
        String a="0";
        TbDevice tbDevice = tbDeviceService.getByCode(deviceCode);
        message.setData(tbDevice);
        return message;
    }

    @GetMapping("getNotRegister")
    public JsonMessage getNotRegister() {
        JsonMessage message = new JsonMessage();
        List<TbDevice> list = tbDeviceService.getNotRegister();
        message.setData(list);
        return message;
    }

    @GetMapping("getRegister")
    public JsonMessage getRegister() {
        JsonMessage message = new JsonMessage();
        List<TbDevice> list = tbDeviceService.getRegister();
        message.setData(list);
        return message;
    }

    /**
     * 设备信息添加
     *
     * @return
     */

    @PostMapping("tbDevicesAdd")
    public JsonMessage tbDevicesAdd(String deviceCode, String deviceName, String deviceSpec,
                                    BigDecimal devicePrice, String deviceLoc, String deviceInfo,
                                    String deviceStatus, String typeId, String typeName,
                                    String isRegister,
                                    @RequestParam("file") MultipartFile file) {
        JsonMessage message = new JsonMessage();
        String maxDeviceCode = tbDeviceService.maxDeviceCode();
        if (maxDeviceCode == null || "".equals(maxDeviceCode)) {

            maxDeviceCode = "0000000000000001";
        } else {
            String newDeviceCode = String.valueOf(Long.parseLong(maxDeviceCode) + 1);
            String size = "";
            for (long i = 0; i < 15 - newDeviceCode.length(); i++) {
                size += "0";
            }
            deviceCode="0"+size+newDeviceCode;
        }

        if (devicePrice == null) devicePrice.setScale(0);
        isRegister = "01";
        // 获取原始名字
        String fileName = file.getOriginalFilename();
        // 获取后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 文件保存路径
        String path = uploadPath + File.separator + "devices";
        String filePath = path + File.separator + deviceCode + suffixName;

        // 文件对象
        File dest = new File(filePath);
        // 判断路径是否存在，如果不存在则创建
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            // 保存到服务器中
            file.transferTo(dest);
            message.setData(filePath);
        } catch (Exception e) {
            e.printStackTrace();
            message.setData("上传图片失败");
            return message;
        }
        TbDevice tbDevice = new TbDevice(deviceCode, deviceName, deviceSpec, devicePrice, deviceLoc, deviceInfo, deviceStatus, typeId, typeName, "0", filePath, 0, isRegister);
        try {
            tbDeviceService.devicesAdd(tbDevice);
            message.setData("添加成功");

        } catch (DuplicateKeyException e) {
            message.setData("设备编号已存在");
        }
        message.setMessage("ok");
        return message;
    }

    /**
     * 设备更新
     *
     * @return
     */
    @PostMapping("tbDeviceUpdate")
    public JsonMessage tbDeviceUpdate(@Param("deviceCode") String deviceCode, @Param("deviceName") String deviceName, @Param("deviceSpec") String deviceSpec,
                                      @Param("devicePrice") BigDecimal devicePrice, @Param("deviceLoc") String deviceLoc, @Param("deviceInfo") String deviceInfo,
                                      @Param("deviceStatus") String deviceStatus, @Param("typeId") String typeId, @Param("typeName") String typeName,
                                      @Param("isBorrow") String isBorrow, @Param("disabled") Integer disabled, @Param("isRegister") String isRegister
    ) {
        JsonMessage message = new JsonMessage();

        TbDevice tbDevice = new TbDevice(deviceCode, deviceName, deviceSpec, devicePrice, deviceLoc, deviceInfo, deviceStatus, typeId, typeName, isBorrow, disabled, isRegister);

        tbDeviceService.devicesUpdate(tbDevice);
        message.setData("修改成功");
        return message;
    }

    /**
     * 编号修改
     *
     * @param deviceCodeOld 旧编号
     * @param deviceCodeNew 新编号
     * @return
     */
    @GetMapping("tbDeviceCodeUpdate")
    public JsonMessage deviceCodeUpdate(String deviceCodeOld, String deviceCodeNew) {
        JsonMessage message = new JsonMessage();
        try {
            tbDeviceService.deviceCodeUpdate(deviceCodeOld, deviceCodeNew);
        } catch (DuplicateKeyException e) {
            message.setData("编号已存在");
        }
        return message;
    }

    /**
     * 删除信息
     *
     * @param deviceCode
     * @return
     */
    @GetMapping("tbDeviceDelete")
    public JsonMessage deviceDelete(String deviceCode) {
        JsonMessage message = new JsonMessage();
        tbDeviceService.deviceDeleteFalse(deviceCode);
        message.setData("删除成功");
        return message;
    }

    /**
     * 所有设备信息
     *
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("tbDeviceList")
    public JsonMessage deviceList(@RequestParam(defaultValue = "1") int pageIndex,
                                  @RequestParam(defaultValue = "10") int pageSize) {
        JsonMessage message = new JsonMessage();
        PageInfo<TbDevice> list = tbDeviceService.tbDeviceList(pageIndex, pageSize);
        message.setData(list);
        return message;
    }

    @GetMapping("level3List")
    public JsonMessage level3List(@RequestParam(defaultValue = "1") int pageIndex,
                                  @RequestParam(defaultValue = "10") int pageSize) {
        JsonMessage message = new JsonMessage();
        PageInfo<TbDevice> list = tbDeviceService.level3(pageIndex, pageSize);
        message.setData(list);
        return message;
    }


    /**
     * 单个设备信息
     *
     * @param deviceCode
     * @return
     */
    @GetMapping("tbDevice")
    public JsonMessage tbDevice(String deviceCode) {
        JsonMessage message = new JsonMessage();
        message.setData(tbDeviceService.tbDevice(deviceCode));
        return message;
    }

    @GetMapping("deviceStatus")
    public JsonMessage deviceStatus(String deviceCode) {
        JsonMessage message = new JsonMessage();
        message.setData(tbDeviceService.deviceStatus(deviceCode));
        return message;
    }


}
